//手机端返回上一步
$('.icon-zuo').on('touchend click', function (e) {
    e.preventDefault();
    window.history.back();
});

//获得/失去焦点样式
$('.register-body input').focus(function () {
    $(this).css({borderColor: "#35a2ff"})
}).blur(function () {
    $(this).css({borderColor: ""})
});

$("#reg_form").validate({
    rules: {
        name: {
            required: true,
            rangelength: [5, 50],
            username: true
        },
        pname: {
            required: true,
            rangelength: [5, 50],
            username: true
        },
        pass: {
            required: true,
            rangelength: [6, 30],
        },
        repass: {
            required: true,
            equalTo: "#pass"
        },
        safepass: {
            required: true,
            rangelength: [6, 30],
        },
        resafepass: {
            required: true,
            equalTo: "#safepass"
        },
        realname: {
            required: true,
            realname: true
        },
        tel: {
            required: true,
            isMobile: true
        },
        sms_code: 'required',
        protocol: {
            required: true
        }
    },
    messages: {
        name: {
            required: '请填写用户名',
            rangelength: '用户名只能5位以上'
        },
        pname: {
            required: '请填写推荐人用户名',
            rangelength: '推荐人用户名只能5位以上'
        },
        pass: {
            required: '请填写登录密码',
            rangelength: '登录密码只能6-30位'
        },
        repass: {
            required: '请确认登录密码',
            equalTo: '登录密码两次输入不一致'
        },
        safepass: {
            required: '请填写交易密码',
            rangelength: '交易密码只能6-30位'
        },
        resafepass: {
            required: '请确认交易密码',
            equalTo: '交易密码两次输入不一致'
        },
        realname: {
            required: '请填写您的真实姓名'
        },
        tel: {
            required: '请输入手机号'
        },
        sms_code: {
            required: '请填写短信验证码'
        },
        protocol: {
            required: '请阅读并同意注册协议'
        }
    }
    , errorPlacement: function (error, element) {
        $(element).siblings('p').html(error);
        return false;
    },
    errorElement: "span",
    submitHandler: function (form) {
        $(form).ajaxSubmit({
            dataType: "json",
            type: "post",
            beforeSend: function () {
                $(".register-ljzc").addClass('yfs').attr('disabled', 'disabled');
            },
            success: function (data) {
                $(".register-ljzc").removeClass('yfs').removeAttr('disabled');
                if (data.code == 0) {
                    toastr["success"](data.msg);
                    setInterval(function () {
                        window.location = "{{route('login.page')}}";
                    }, 2000);
                } else {
                    toastr["error"](data.msg);
                }
            }
        });
    }
});


$('#sendSmsCode').on('touchend click', function (e) {
    e.preventDefault();
    //手机号验证
    var tel = $("#tel").val();
    var rule = /^[1][34578][\d]{9}$/.test(tel);
    if (rule) {
        $("#tel").removeClass('error').siblings('p').html('')
    } else {
        $("#tel").addClass('error').siblings('p').html('请输入正确手机号');
        return false;
    }

    var self = $("#sendSmsCode");
    $.ajax({
        type: "POST",
        url: "/send-common-code/" + tel + "/" + "reg",
        dataType: "json",
        beforeSend: function () {
            $('.register-hqyzm').addClass('yfs').attr('disabled', 'disabled');
        },
        success: function (data) {
            if (data.code == 0) {
                toastr.success(data.msg);
                var num = 120, timer;
                $(this).addClass('yfs').val('重新发送（' + num + '）');
                timer = setInterval(function () {
                    num--;
                    $('.register-hqyzm').val('重新发送（' + num + '）');
                    if (num <= 0) {
                        clearInterval(timer);
                        $('.register-hqyzm').removeClass('yfs').removeAttr('disabled').val('获取验证码');
                    }
                }, 1000);
            }
            if (data.code == 1) {
                toastr.error(data.msg);
            }
        }
    });
});

$('.register-xy i').on('touchend click', function (e) {
    e.preventDefault();
    if ($(this).hasClass('icon-iconziti50')) {
        $(this).removeClass('icon-iconziti50').addClass('icon-xuanzhong');
        $("#protocol").val('agree');
    } else {
        $(this).removeClass('icon-xuanzhong').addClass('icon-iconziti50');
        $("#protocol").val('');
    }
});